"""
@name : 一刀九九九
@author : wangshenghu
@projectname: tlbb
@Date  : 2022/7/7
"""


class Character():
    # 初始化角色属性
    def __init__(self, name, blood, equip):
        self.name = name
        self.blood = blood
        self.equip = equip
        self.room = None

    # fight方法里面传入一个和自己战斗的对象
    def fight(self, brother):
        # 确保对战玩家在同一房间
        if self.room == brother.room:
            # 不能和自己对战
            if self != brother:
                if self.blood > 0 and brother.blood > 0:
                    harmful = 5 if brother.equip == '防弹衣' else 10
                    brother.blood -= harmful
                    print(f"{self.name}攻击{brother.name}造成伤害:{harmful},{brother.name}剩余血量：{brother.blood}")
                else:
                    print("一方英雄血量为0，游戏结束！")
                    exit()
            else:
                print("不能和自己对战！")
        else:
            print("玩家不在同一房间！")


# 创建一个容器存储角色实例
roles = {}
# 创建一个容器存储房间实例
rooms = {}


class Room():
    def __init__(self, name):
        self.room_name = name
        # 定义一个列表merber来储存房间里的角色
        self.merber = []

    def joinroom(self, role):
        # 判断角色的房间属性是否为空
        if role.room == None:
            if len(self.merber) < 2:
                # 往房间添加玩家
                self.merber.append(role)
                role.room = self.room_name
            else:
                print("此房间已满！")
        else:
            print("用户已经加入其它房间！")


while True:
    print("欢迎来到一刀10滴血小游戏")
    print("/**菜单**/".center(50, "#"))
    print("1.创建角色")
    print("2.创建房间")
    print("3.加入房间")
    print("4.开始游戏")
    print("5.退出游戏")
    choices = int(input("请输入您的选择："))
    if choices == 1:
        chara_name = input("请输入角色名字：")
        chara_blood = int(input("请输入角色血量："))
        chara_equip = input("请问是否穿戴防弹衣：(y/n)")
        # 初始化防弹衣状态
        if chara_equip.lower() == 'y':
            chara_equip = "防弹衣"
        elif chara_equip.lower() == 'n':
            chara_equip = "无"
        # roles.update({'角色姓名': chara_name,
        #               '血量': chara_blood, '装备': chara_equip})
        # 创建角色实例
        roles[chara_name] = Character(chara_name, chara_blood, chara_equip)
        print("角色信息初始化成功！")
        print(f"{chara_name} 当前血量：{roles[chara_name].blood} 装备:{roles[chara_name].equip}")

    elif choices == 2:
        room_name = input("请输入要创建的房间名称：")
        # 创建房间实例
        rooms[room_name] = Room(room_name)
    elif choices == 3:
        print(f"现有角色：{roles.keys()} 现有房间：{rooms.keys()}")
        name_option = input("请输入要加入的角色：")
        room_option = input("请输入要加入的房间：")
        # 将选择的角色添加到选择的房间
        rooms[room_option].joinroom(roles[name_option])
    elif choices == 4:
        print(f"现有房间:{rooms.keys()}")
        fight_room = input("请输入开始战斗的房间：")
        # 确保开始战斗的房间有两个人。
        if len(rooms[fight_room].merber) == 2:
            while True:
                rooms[fight_room].merber[0].fight(rooms[fight_room].merber[1])
                rooms[fight_room].merber[1].fight(rooms[fight_room].merber[0])
        else:
            print("房间人数不足两人，无法战斗")
    elif choices == 5:
        break
    else:
        print("您的输入有误，请重新输入！")